package com.tobeface.wechat.request;

import java.util.concurrent.TimeUnit;

/**
 * @author loudyn
 */
public final class WeChatToken {

    private final String token;
    private final Long expiredTimeMillis;

    WeChatToken(Builder builder) {
        this.token = builder.token;
        this.expiredTimeMillis = builder.expiredTimeMillis;
    }

    public String getToken() {
        return token;
    }

    public Long getExpiredTimeMillis() {
        return expiredTimeMillis;
    }

    /**
     * @return
     */
    public boolean isExpired() {
        return System.currentTimeMillis() > getExpiredTimeMillis();
    }

    /**
     *
     * @return
     */
    public static Builder newBuilder() {
        return new Builder();
    }

    /**
     *
     */
    public static class Builder {

        Builder() {
        }

        private String token;
        private long expiredTimeMillis;

        public Builder token(String token) {
            this.token = token;
            return this;
        }

        /**
         * @param afterSeconds
         * @return
         */
        public Builder expiredAfterSeconds(long afterSeconds) {
            return expiredOn(System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(afterSeconds));
        }

        /**
         * @param expiredTimeMillis
         * @return
         */
        public Builder expiredOn(long expiredTimeMillis) {
            this.expiredTimeMillis = expiredTimeMillis;
            return this;
        }

        /**
         * @return
         */
        public WeChatToken build() {
            return new WeChatToken(this);
        }
    }
}